package com.leoman.controller.backend;

import com.leoman.common.DataTableFactory;
import com.leoman.controller.common.CommonController;
import com.leoman.entity.Banner;
import com.leoman.service.BannerService;
import com.leoman.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 广告管理
 * Created by 涂奕恒 on 2016/10/30.
 */
@Controller
@RequestMapping(value = "backend/banner")
public class BannerController extends CommonController {

    @Autowired
    private BannerService bannerService;

    @RequestMapping("index")
    public String index() {
        return "backend/广告列表";
    }

    @RequestMapping("add")
    public String add(ModelMap model, Integer bannerId) {
        if (null != bannerId) {
            model.addAttribute("banner", bannerService.getById(bannerId));
        }
        return "backend/广告详情";
    }

    @RequestMapping("/list")
    public void list(HttpServletResponse response,
                     Integer draw,
                     Integer start,
                     Integer length) {
        if (null == start || start == 0) {
            start = 1;
        }
        int pageNum = getPageNum(start, length);
        Page<Banner> page = bannerService.find(pageNum, length);

        Map<String, Object> result = DataTableFactory.fitting(draw, page);
        WebUtil.printJson(response, result);
    }

    /**
     * 新增 or 编辑广告信息
     *
     * @param bannerId         广告ID
     * @param linkUrl          外链地址
     * @param videoTypeId      关联视频分类ID
     * @param videoId          关联视频ID
     * @param multipartRequest 广告封面
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public Integer save(Integer bannerId, String linkUrl, Integer videoTypeId, Integer videoId, MultipartRequest multipartRequest) {
        try {
            return bannerService.saveBanner(bannerId, linkUrl, videoTypeId, videoId, multipartRequest);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 删除广告信息
     *
     * @param bannerId 广告ID
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Integer delete(Integer bannerId) {
        try {
            bannerService.deleteById(bannerId);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 批量删除广告信息
     *
     * @param ids 广告ID
     * @return
     */
    @RequestMapping("/batchDelete")
    @ResponseBody
    public Integer batchDelete(String ids) {
        try {
            return bannerService.batchDelete(ids);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
